<?php
require_once("thamdinh/models/tvgs.php");
require_once("thamdinh/models/tvtk.php");
require_once("thamdinh/models/tochuc.php");
require_once("thamdinh/models/linhvuc.php");
require_once("thamdinh/models/level.php");
require_once("thamdinh/models/formTVTK.php");
require_once("thamdinh/models/formTVGS.php");
class IndexController extends GenericController {

    public function indexAction() {
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
            $this->_helper->layout->disableLayout();
        } else {
            $this->_helper->layout->enableLayout();
        }
    }

    public function tvtkbuoc1Action() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();

        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        //var_dump($this->view->tochuc["ID_TC"]);
        $this->view->tvtk = TVTK::SelectLastTVTKByID_TC($this->view->tochuc["ID_TC"]);
    }

    public function tvtkbuoc1savedraftAction() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        $params = $this->getRequest()->getParams();

        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        $this->view->tvtk = TVTK::SelectLastTVTKByID_TC($this->view->tochuc["ID_TC"]);

        // Save thong tin to chuc
        $parameter = Array(
            "NAME" => $params["NAMETOCHUC"]
            , "GPDKKD_SO" => $params["GPDKKD_SO"]
            , "GPDKKD_NGAY" => TVTKCommon::VNDateToMySQLDate($params["GPDKKD_NGAY"])
            , "TRUSO" => $params["TRUSO"]
            , "DIENTHOAI" => $params["DIENTHOAI"]
            , "DAIDIEN" => $params["DAIDIEN"]
            , "FAX" => $params["FAX"]
        );
        if((int)$this->view->tochuc["ID_TC"]>0)
        {
            ToChuc::UpdateByID_TC($parameter, $this->view->tochuc["ID_TC"]);
        }
        else {
            $parameter['ID_U']=$user->ID_U;
            $toChucModel= new ToChuc();
            $toChucModel->insert($parameter);            
            $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
            $this->view->tvtk = TVTK::SelectLastTVTKByID_TC($this->view->tochuc["ID_TC"]);
        }

        //save bm1 khi chua co bm1 nao (tất nhiên chỉ là lưu nháp)
        if ((int)$this->view->tvtk["ID_BK1"] == 0) {
            $parameter = Array(
                "ID_LV" => $params["ID_LV"]
                , "SONAMHOATDONG" => (int)$params["SONAMHOATDONG"]
                , "DIEM_SONAMHOATDONG" => (int)$params["DIEM_SONAMHOATDONG"]
            );
            TVTK::CreateTVTKM1WithStatusIsDraft($parameter, $this->view->tochuc["ID_TC"]);
        } else {
            // nếu status là -1 thì update
            if ($this->view->tvtk["STATUS"] == -1) {
                $parameter = Array(
                    "ID_LV" => $params["ID_LV"]
                    , "SONAMHOATDONG" => (int)$params["SONAMHOATDONG"]
                    , "DIEM_SONAMHOATDONG" => (int)$params["DIEM_SONAMHOATDONG"]
                );
                TVTK::UpdateTVTKM1Buoc1($parameter, $this->view->tvtk["ID_BK1"]);
            } else {
                // nếu là 0,1,... thì thêm mới (đã duyệt hoặc đã gì gì đó)
                // copy nang luc nhan su
                $idbm1old = $this->view->tvtk["ID_BK1"];

                $parameter = Array(
                    "ID_LV" => $params["ID_LV"]
                    , "SONAMHOATDONG" => (int)$params["SONAMHOATDONG"]
                    , "DIEM_SONAMHOATDONG" => (int)$params["DIEM_SONAMHOATDONG"]
                );
                $idbm1new = TVTK::CreateTVTKM1WithStatusIsDraft($parameter, $this->view->tochuc["ID_TC"]);

                TVTK::CopyNangLucNhanSuListByID_BM1($idbm1old, $idbm1new);
                TVTK::CopyNangLucKhacListByID_BM1($idbm1old, $idbm1new);
                TVTK::CopyDoanhThuListByID_BM1($idbm1old, $idbm1new);
                TVTK::CopyDuAnListByID_BM1($idbm1old, $idbm1new);
            }
        }

        echo 1;
        exit;
    }

    public function tvtkbuoc2Action() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();

        $this->view->level = Level::SelectLevelListByTYPE("TVTK_CAPHANG");
        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        //var_dump($this->view->tochuc["ID_TC"]);
        $this->view->tvtk = TVTK::SelectLastTVTKByID_TC($this->view->tochuc["ID_TC"]);  
        //var_dump($this->view->tvtk["ID_BK1"]);
        $this->view->nlns = TVTK::SelectNangLucNhanSuListByID_BM1($this->view->tvtk["ID_BK1"]);
    }

    public function tvtkbuoc2savedraftAction() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        $params = $this->getRequest()->getParams();

        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        $this->view->tvtk = TVTK::SelectLastTVTKByID_TC($this->view->tochuc["ID_TC"]);
        $is_new = 0;

        if ($params["delete"] > 0) {
            $nlns = TVTK::SelectNangLucNhanSuByID_NLNS($params["delete"], $this->view->tochuc["ID_TC"]);
            if ($nlns["STATUS"] == 2) {
                TVTK::DeleteNangLucNhanSuByID_NLNS($params["delete"], $this->view->tvtk["ID_BK1"], 0);
                $is_new = 2;
            } else {
                TVTK::DeleteNangLucNhanSuByID_NLNS($params["delete"], $this->view->tvtk["ID_BK1"], 1);
            }

            $params["ID_NLNS"] = $params["delete"];
        } else {
            // validate
            if ($params["ID_LV"] == 0) {
                $nlns = Array();
                $nlns["msg"] = "Chưa nhập Hạng";
                echo json_encode($nlns);
                exit;
            }

            if ($params["DIEM"] == 0) {
                $nlns = Array();
                $nlns["msg"] = "Chưa nhập Điểm";
                echo json_encode($nlns);
                exit;
            }

            if ($params["HOTEN"] == "") {
                $nlns = Array();
                $nlns["msg"] = "Chưa nhập Họ tên";
                echo json_encode($nlns);
                exit;
            }
            // che do edit
            if ($params["ID_NLNS"] > 0) {
                $parameter = Array();
                $parameter["HOTEN"] = $params["HOTEN"];
                $parameter["CMND"] = $params["CMND"];
                $parameter["NGHE"] = $params["NGHE"];
                $parameter["SOCHUNGCHIHANHNGHE"] = $params["SOCHUNGCHIHANHNGHE"];
                $parameter["BAOHIEMXAHOI_SO"] = $params["BAOHIEMXAHOI_SO"];
                $parameter["BAOHIEMXAHOI_NGAY"] = TVTKCommon::VNDateToMySQLDate($params["BAOHIEMXAHOI_NGAY"]);
                $parameter["BAOHIEMXAHOI_NOICAP"] = $params["BAOHIEMXAHOI_NOICAP"];
                $parameter["ID_LV"] = $params["ID_LV"];
                $parameter["DIEM"] = $params["DIEM"];
                $parameter["GHICHU"] = $params["GHICHU"];
                TVTK::UpdateNangLucNhanSuByID_NLNS($parameter, $params["ID_NLNS"], $this->view->tvtk["ID_BK1"]);
            } else {
                $parameter = Array();
                $parameter["HOTEN"] = $params["HOTEN"];
                $parameter["CMND"] = $params["CMND"];
                $parameter["NGHE"] = $params["NGHE"];
                $parameter["SOCHUNGCHIHANHNGHE"] = $params["SOCHUNGCHIHANHNGHE"];
                $parameter["BAOHIEMXAHOI_SO"] = $params["BAOHIEMXAHOI_SO"];
                $parameter["BAOHIEMXAHOI_NGAY"] = TVTKCommon::VNDateToMySQLDate($params["BAOHIEMXAHOI_NGAY"]);
                $parameter["BAOHIEMXAHOI_NOICAP"] = $params["BAOHIEMXAHOI_NOICAP"];
                $parameter["ID_LV"] = $params["ID_LV"];
                $parameter["DIEM"] = $params["DIEM"];
                $parameter["GHICHU"] = $params["GHICHU"];
                $params["ID_NLNS"] = TVTK::InsertNangLucNhanSuByID_NLNS($parameter, $this->view->tvtk["ID_BK1"]);
                $is_new = 1;
            }
        }

        // Update DIEM_NANGLUCNHANSU
        TVTK::UpdateTVTKM1DIEM_NANGLUCNHANSU($this->view->tvtk["ID_BK1"]);

        if ($is_new != 2) {
            $nlns = TVTK::SelectNangLucNhanSuByID_NLNS($params["ID_NLNS"], $this->view->tochuc["ID_TC"]);
        } else {
            $nlns = Array();
            $nlns["ID_NLNS"] = $params["ID_NLNS"];
        }
        $nlns["IS_NEW"] = $is_new;
        echo json_encode($nlns);

        exit;
    }

    public function tvtkbuoc2loadnlnsAction() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        $params = $this->getRequest()->getParams();

        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);

        // Chi load nhan su neu dung la cua to chuc dang login
        $nlns = TVTK::SelectNangLucNhanSuByID_NLNS($params["id"], $this->view->tochuc["ID_TC"]);
        echo json_encode($nlns);
        exit;
    }

    public function tvtkbuoc3Action() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();

        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        $this->view->tvtk = TVTK::SelectLastTVTKByID_TC($this->view->tochuc["ID_TC"]);
        $this->view->level = TVTK::SelectNangLucKhacListByID_BM1($this->view->tvtk["ID_BK1"]);
        $this->view->doanhthu = TVTK::SelectDoanhThuListByID_BM1($this->view->tvtk["ID_BK1"], date("Y") - 4, date("Y"));
    }

    public function tvtkbuoc3savedraftAction() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        $params = $this->getRequest()->getParams();

        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        $this->view->tvtk = TVTK::SelectLastTVTKByID_TC($this->view->tochuc["ID_TC"]);
        $this->view->level = TVTK::SelectNangLucKhacListByID_BM1($this->view->tvtk["ID_BK1"]);
        $this->view->doanhthu = TVTK::SelectDoanhThuListByID_BM1($this->view->tvtk["ID_BK1"], date("Y") - 4, date("Y"));

        // get diem tvtk
        foreach ($this->view->level as $itemlevel) {
            TVTK::UpdateNangLucKhacByID_LV($itemlevel["ID_NLK"], $itemlevel["ID_LV"], $params["TVTKKHACDIEM" . $itemlevel["ID_LV"]], $params["TVTKKHACGHICHU" . $itemlevel["ID_LV"]], $this->view->tvtk["ID_BK1"]);
        }

        $trungbinh5nam = 0;
        for ($y = date('Y') - 4; $y <= date('Y'); $y++) {
            TVTK::UpdateDoanhThuByID_BM1($this->view->tvtk["ID_BK1"], $y, $params["ID_DT_YEAR_" . $y], $this->view->tochuc["ID_TC"]);
            $trungbinh5nam += $params["ID_DT_YEAR_" . $y];
        }

        $trungbinh5nam = ($trungbinh5nam / 5.0) * 2.0;

        TVTK::UpdateTVTKM1DIEM_DOANHTHU(Array("DIEM_DOANHTHU" => $trungbinh5nam), $this->view->tvtk["ID_BK1"]);

        echo 1;
        exit;
    }

    public function tvtkbuoc4Action() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();

        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        $this->view->tvtk = TVTK::SelectLastTVTKByID_TC($this->view->tochuc["ID_TC"]);
        $this->view->duan = TVTK::SelectDuAnListByID_BM1($this->view->tvtk["ID_BK1"], date("Y") - 4, date("Y"));

        $this->view->countlevelnhomcapcongtrinh = Level::SelectLevelCountByTYPE('TVTK_NHOMCAPCONGTRINH');
        $this->view->levelnhomcapcongtrinh = Level::SelectLevelListByTYPE('TVTK_NHOMCAPCONGTRINH');

        $this->view->countlevelthongtindichvu = Level::SelectLevelCountByTYPE('TVTK_THONGTINDICHVU');
        $this->view->levelthongtindichvu = Level::SelectLevelListByTYPE('TVTK_THONGTINDICHVU');

        $this->view->countlevelthongtinbosung = Level::SelectLevelCountByTYPE('TVTK_THONGTINBOSUNG');
        $this->view->levelthongtinbosung = Level::SelectLevelListByTYPE('TVTK_THONGTINBOSUNG');
    }
    
    public function tvtkbuoc4savedraftAction() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        $params = $this->getRequest()->getParams();

        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        $this->view->tvtk = TVTK::SelectLastTVTKByID_TC($this->view->tochuc["ID_TC"]);
        $is_new = 0;

        if ($params["delete"] > 0) {
            $duan = TVTK::SelectDuAnByID_DuAn($params["delete"], $this->view->tochuc["ID_TC"]);
            if ($duan["STATUS"] == 2) {
                TVTK::DeleteDuAnByID_DuAn($params["delete"], $this->view->tvtk["ID_BK1"], 0);
                $is_new = 2;
            } else {
                TVTK::DeleteDuAnByID_DuAn($params["delete"], $this->view->tvtk["ID_BK1"], 1);
            }

            $params["ID_DUAN"] = $params["delete"];
        } else {
            
            if ($params["DIEM_DUAN"] == 0) {
                $duan = Array();
                $duan["msg"] = "Chưa nhập Điểm";
                echo json_encode($duan);
                exit;
            }

            if ($params["TENDUAN"] == "") {
                $duan = Array();
                $duan["msg"] = "Chưa nhập tên dự án";
                echo json_encode($duan);
                exit;
            }
            // che do edit
            if ($params["ID_DUAN"] > 0) {
                $parameter = Array();
                $parameter["TENDUAN"] = $params["TENDUAN"];
                $parameter["NAMDUAN"] = $params["NAMDUAN"];
                $parameter["CAPDAUTU"] = $params["CAPDAUTU"];
                $parameter["CHUDAUTU"] = $params["CHUDAUTU"];
                $parameter["DAIDIENDAUTU"] = $params["DAIDIENDAUTU"];
                $parameter["LOAITYPE"] = $params["LOAITYPE"];
                $parameter["NHOMCAPCONGTRINH"] = $params["NHOMCAPCONGTRINH"];
                $parameter["GHICHU_NHOMCAPCONGTRINH"] = $params["GHICHU_NHOMCAPCONGTRINH"];
                $parameter["THONGTINDICHVU"] = $params["THONGTINDICHVU"];                
                $parameter["GHICHU_THONGTINDICHVU"] = $params["GHICHU_THONGTINDICHVU"];
                $parameter["THONGTINBOSUNG"] = $params["THONGTINBOSUNG"];
                $parameter["GHICHU_THONGTINBOSUNG"] = $params["GHICHU_THONGTINBOSUNG"];
                $parameter["DIEM_DUAN"] = $params["DIEM_DUAN"];
                TVTK::UpdateDuAnByID_DUAN($parameter, $params["ID_DUAN"], $this->view->tvtk["ID_BK1"]);
            } else {
                $parameter = Array();
                $parameter["TENDUAN"] = $params["TENDUAN"];
                $parameter["NAMDUAN"] = $params["NAMDUAN"];
                $parameter["CAPDAUTU"] = $params["CAPDAUTU"];
                $parameter["CHUDAUTU"] = $params["CHUDAUTU"];
                $parameter["DAIDIENDAUTU"] = $params["DAIDIENDAUTU"];
                $parameter["LOAITYPE"] = $params["LOAITYPE"];
                $parameter["NHOMCAPCONGTRINH"] = $params["NHOMCAPCONGTRINH"];
                $parameter["GHICHU_NHOMCAPCONGTRINH"] = $params["GHICHU_NHOMCAPCONGTRINH"];
                $parameter["THONGTINDICHVU"] = $params["THONGTINDICHVU"];                
                $parameter["GHICHU_THONGTINDICHVU"] = $params["GHICHU_THONGTINDICHVU"];
                $parameter["THONGTINBOSUNG"] = $params["THONGTINBOSUNG"];
                $parameter["GHICHU_THONGTINBOSUNG"] = $params["GHICHU_THONGTINBOSUNG"];
                $parameter["DIEM_DUAN"] = $params["DIEM_DUAN"];
                $params["ID_DUAN"] = TVTK::InsertDuAnByID_DUAN($parameter, $this->view->tvtk["ID_BK1"]);
                $is_new = 1;
            }
        }

        // Update DIEMduan
        TVTK::UpdateTVTKM1DIEM_DUAN($this->view->tvtk["ID_BK1"]);

        if ($is_new != 2) {
            $duan = TVTK::SelectDuAnByID_DuAn($params["ID_DUAN"], $this->view->tochuc["ID_TC"]);
        } else {
            $duan = Array();
            $duan["ID_DUAN"] = $params["ID_DUAN"];
        }
        $duan["IS_NEW"] = $is_new;
        echo json_encode($duan);

        exit;
    }


    public function tvtkbuoc4loadduanAction() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        $params = $this->getRequest()->getParams();

        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);

        // Chi load duan neu dung la cua to chuc dang login
        $duan= TVTK::SelectDuAnByID_DuAn($params["id"], $this->view->tochuc["ID_TC"]);
        echo json_encode($duan);
        exit;
    }
    public function viewtvtktrang1Action() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVTK::SelectTVTKByID($id);
            $this->view->level = Level::SelectLevelListByTYPE("TVTK_CAPHANG");
            $this->view->levelnangluckhac = TVTK::SelectNangLucKhacListByID_BM1($id);
            $this->view->doanhthu = TVTK::SelectDoanhThuListByID_BM1($id, date("Y") - 4, date("Y"));
            $this->view->nlns = TVTK::SelectNangLucNhanSuListByID_BM1($id);
        }
        else
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
            $this->view->tvtk = TVTK::SelectLastTVTKByID_TC($this->view->tochuc["ID_TC"]);
            $this->view->level = Level::SelectLevelListByTYPE("TVTK_CAPHANG");
            $this->view->levelnangluckhac = TVTK::SelectNangLucKhacListByID_BM1($this->view->tvtk["ID_BK1"]);
            $this->view->doanhthu = TVTK::SelectDoanhThuListByID_BM1($this->view->tvtk["ID_BK1"], date("Y") - 4, date("Y"));
            $this->view->nlns = TVTK::SelectNangLucNhanSuListByID_BM1($this->view->tvtk["ID_BK1"]);
        }
        $this->view->id = $id;
        $this->view->readonly = (int)$this->getRequest()->getParam('readonly');
    }

    public function viewtvtktrang2Action() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVTK::SelectTVTKByID($id);
            $this->view->duan = TVTK::SelectDuAnListByID_BM1($id, date("Y") - 4, date("Y"));
              
        }
        else
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
            $this->view->tvtk = TVTK::SelectLastTVTKByID_TC($this->view->tochuc["ID_TC"]);
            $this->view->duan = TVTK::SelectDuAnListByID_BM1($this->view->tvtk["ID_BK1"], date("Y") - 4, date("Y"));
        }
        $this->view->countlevelnhomcapcongtrinh = Level::SelectLevelCountByTYPE('TVTK_NHOMCAPCONGTRINH');
        $this->view->levelnhomcapcongtrinh = Level::SelectLevelListByTYPE('TVTK_NHOMCAPCONGTRINH');
        $this->view->countlevelthongtindichvu = Level::SelectLevelCountByTYPE('TVTK_THONGTINDICHVU');
        $this->view->levelthongtindichvu = Level::SelectLevelListByTYPE('TVTK_THONGTINDICHVU');
        $this->view->countlevelthongtinbosung = Level::SelectLevelCountByTYPE('TVTK_THONGTINBOSUNG');
        $this->view->levelthongtinbosung = Level::SelectLevelListByTYPE('TVTK_THONGTINBOSUNG');
        $this->view->id = $id;
        $this->view->readonly = (int)$this->getRequest()->getParam('readonly');
    }
    public function viewexceltvtktrang1Action() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        header("Content-Type: application/vnd.ms-excel; name='excel'");
        header("Content-Disposition: attachment; filename=tvtktrang1.xls;");
        header("Pragma: no-cache");
        header("Expires: 0");
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVTK::SelectTVTKByID($id);
            $this->view->level = Level::SelectLevelListByTYPE("TVTK_CAPHANG");
            $this->view->levelnangluckhac = TVTK::SelectNangLucKhacListByID_BM1($id);
            $this->view->doanhthu = TVTK::SelectDoanhThuListByID_BM1($id, date("Y") - 4, date("Y"));
            $this->view->nlns = TVTK::SelectNangLucNhanSuListByID_BM1($id);
        }
        else
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
            $this->view->tvtk = TVTK::SelectLastTVTKByID_TC($this->view->tochuc["ID_TC"]);
            $this->view->level = Level::SelectLevelListByTYPE("TVTK_CAPHANG");
            $this->view->levelnangluckhac = TVTK::SelectNangLucKhacListByID_BM1($this->view->tvtk["ID_BK1"]);
            $this->view->doanhthu = TVTK::SelectDoanhThuListByID_BM1($this->view->tvtk["ID_BK1"], date("Y") - 4, date("Y"));
            $this->view->nlns = TVTK::SelectNangLucNhanSuListByID_BM1($this->view->tvtk["ID_BK1"]);
        }
        $this->view->id = $id;
        $this->view->readonly = (int)$this->getRequest()->getParam('readonly');
    }

    public function viewexceltvtktrang2Action() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        header("Content-Type: application/vnd.ms-excel; name='excel'");
        header("Content-Disposition: attachment; filename=tvtktrang2.xls;");
        header("Pragma: no-cache");
        header("Expires: 0");
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVTK::SelectTVTKByID($id);
            $this->view->duan = TVTK::SelectDuAnListByID_BM1($id, date("Y") - 4, date("Y"));
              
        }
        else
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
            $this->view->tvtk = TVTK::SelectLastTVTKByID_TC($this->view->tochuc["ID_TC"]);
            $this->view->duan = TVTK::SelectDuAnListByID_BM1($this->view->tvtk["ID_BK1"], date("Y") - 4, date("Y"));
        }
        $this->view->countlevelnhomcapcongtrinh = Level::SelectLevelCountByTYPE('TVTK_NHOMCAPCONGTRINH');
        $this->view->levelnhomcapcongtrinh = Level::SelectLevelListByTYPE('TVTK_NHOMCAPCONGTRINH');
        $this->view->countlevelthongtindichvu = Level::SelectLevelCountByTYPE('TVTK_THONGTINDICHVU');
        $this->view->levelthongtindichvu = Level::SelectLevelListByTYPE('TVTK_THONGTINDICHVU');
        $this->view->countlevelthongtinbosung = Level::SelectLevelCountByTYPE('TVTK_THONGTINBOSUNG');
        $this->view->levelthongtinbosung = Level::SelectLevelListByTYPE('TVTK_THONGTINBOSUNG');
        $this->view->id = $id;
        $this->view->readonly = (int)$this->getRequest()->getParam('readonly');
    }
    public function nopdonAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        
        $this->view->tvtk = TVTK::SelectLastTVTKByID_TC($this->view->tochuc["ID_TC"]);
        if((int)$this->view->tvtk["ID_BK1"] != 0)
        {
            //update status nop don = 0 
            $data=array('STATUS'=>0);
            $model= new  formTVTK();
            $model->update($data, 'ID_BK1='.$this->view->tvtk["ID_BK1"]);
            $this->_redirect('/');
        }        
    }
    /*
     * ***************************************************************************************
     */
    
    public function tvgsbuoc1Action() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();

        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        //var_dump($this->view->tochuc["ID_TC"]);
        $this->view->tvtk = TVGS::SelectLastTVGSByID_TC($this->view->tochuc["ID_TC"]);
    }

    public function tvgsbuoc1savedraftAction() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        $params = $this->getRequest()->getParams();

        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        $this->view->tvtk = TVGS::SelectLastTVGSByID_TC($this->view->tochuc["ID_TC"]);

        // Save thong tin to chuc
        $parameter = Array(
            "NAME" => $params["NAMETOCHUC"]
            , "GPDKKD_SO" => $params["GPDKKD_SO"]
            , "GPDKKD_NGAY" => TVTKCommon::VNDateToMySQLDate($params["GPDKKD_NGAY"])
            , "TRUSO" => $params["TRUSO"]
            , "DIENTHOAI" => $params["DIENTHOAI"]
            , "DAIDIEN" => $params["DAIDIEN"]
            , "FAX" => $params["FAX"]
        );
        if((int)$this->view->tochuc["ID_TC"]>0)
        {
            ToChuc::UpdateByID_TC($parameter, $this->view->tochuc["ID_TC"]);
        }
        else {
            $parameter['ID_U']=$user->ID_U;
            $toChucModel= new ToChuc();
            $toChucModel->insert($parameter);            
            $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
            $this->view->tvtk = TVGS::SelectLastTVGSByID_TC($this->view->tochuc["ID_TC"]);
        }

        //save bm1 khi chua co bm1 nao (tất nhiên chỉ là lưu nháp)
        if ((int)$this->view->tvtk["ID_BK2"] == 0) {
            $parameter = Array(
                "ID_LV" => $params["ID_LV"]
                , "SONAMHOATDONG" => (int)$params["SONAMHOATDONG"]
                , "DIEM_SONAMHOATDONG" => (int)$params["DIEM_SONAMHOATDONG"]
            );
            TVGS::CreateTVGSM2WithStatusIsDraft($parameter, $this->view->tochuc["ID_TC"]);
        } else {
            // nếu status là -1 thì update
            if ($this->view->tvtk["STATUS"] == -1) {
                $parameter = Array(
                    "ID_LV" => $params["ID_LV"]
                    , "SONAMHOATDONG" => (int)$params["SONAMHOATDONG"]
                    , "DIEM_SONAMHOATDONG" => (int)$params["DIEM_SONAMHOATDONG"]
                );
                TVGS::UpdateTVGSM2Buoc1($parameter, $this->view->tvtk["ID_BK2"]);
            } else {
                // nếu là 0,1,... thì thêm mới (đã duyệt hoặc đã gì gì đó)
                // copy nang luc nhan su
                $idbm1old = $this->view->tvtk["ID_BK1"];

                $parameter = Array(
                    "ID_LV" => $params["ID_LV"]
                    , "SONAMHOATDONG" => (int)$params["SONAMHOATDONG"]
                    , "DIEM_SONAMHOATDONG" => (int)$params["DIEM_SONAMHOATDONG"]
                );
                $idbm1new = TVGS::CreateTVGSM2WithStatusIsDraft($parameter, $this->view->tochuc["ID_TC"]);

                TVGS::CopyNangLucNhanSuListByID_BM2($idbm1old, $idbm1new);
                TVGS::CopyNangLucKhacListByID_BM2($idbm1old, $idbm1new);
                TVGS::CopyDoanhThuListByID_BM2($idbm1old, $idbm1new);
                TVGS::CopyDuAnListByID_BM2($idbm1old, $idbm1new);
            }
        }

        echo 1;
        exit;
    }

    public function tvgsbuoc2Action() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();

        $this->view->level = Level::SelectLevelListByTYPE("TVTK_CAPHANG");
        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        //var_dump($this->view->tochuc["ID_TC"]);
        $this->view->tvtk = TVGS::SelectLastTVGSByID_TC($this->view->tochuc["ID_TC"]);  
        //var_dump($this->view->tvtk["ID_BK1"]);
        $this->view->nlns = TVGS::SelectNangLucNhanSuListByID_BM2($this->view->tvtk["ID_BK2"]);
    }

    public function tvgsbuoc2savedraftAction() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        $params = $this->getRequest()->getParams();

        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        $this->view->tvtk = TVGS::SelectLastTVGSByID_TC($this->view->tochuc["ID_TC"]);
        $is_new = 0;

        if ($params["delete"] > 0) {
            $nlns = TVGS::SelectNangLucNhanSuByID_NLNS($params["delete"], $this->view->tochuc["ID_TC"]);
            if ($nlns["STATUS"] == 2) {
                TVGS::DeleteNangLucNhanSuByID_NLNS($params["delete"], $this->view->tvtk["ID_BK2"], 0);
                $is_new = 2;
            } else {
                TVGS::DeleteNangLucNhanSuByID_NLNS($params["delete"], $this->view->tvtk["ID_BK2"], 1);
            }

            $params["ID_NLNS"] = $params["delete"];
        } else {
            // validate
            if ($params["ID_LV"] == 0) {
                $nlns = Array();
                $nlns["msg"] = "Chưa nhập Hạng";
                echo json_encode($nlns);
                exit;
            }

            if ($params["DIEM"] == 0) {
                $nlns = Array();
                $nlns["msg"] = "Chưa nhập Điểm";
                echo json_encode($nlns);
                exit;
            }

            if ($params["HOTEN"] == "") {
                $nlns = Array();
                $nlns["msg"] = "Chưa nhập Họ tên";
                echo json_encode($nlns);
                exit;
            }
            // che do edit
            if ($params["ID_NLNS"] > 0) {
                $parameter = Array();
                $parameter["HOTEN"] = $params["HOTEN"];
                $parameter["CMND"] = $params["CMND"];
                $parameter["NGHE"] = $params["NGHE"];
                $parameter["SOCHUNGCHIHANHNGHE"] = $params["SOCHUNGCHIHANHNGHE"];
                $parameter["BAOHIEMXAHOI_SO"] = $params["BAOHIEMXAHOI_SO"];
                $parameter["BAOHIEMXAHOI_NGAY"] = TVTKCommon::VNDateToMySQLDate($params["BAOHIEMXAHOI_NGAY"]);
                $parameter["BAOHIEMXAHOI_NOICAP"] = $params["BAOHIEMXAHOI_NOICAP"];
                $parameter["ID_LV"] = $params["ID_LV"];
                $parameter["DIEM"] = $params["DIEM"];
                $parameter["GHICHU"] = $params["GHICHU"];
                TVGS::UpdateNangLucNhanSuByID_NLNS($parameter, $params["ID_NLNS"], $this->view->tvtk["ID_BK2"]);
            } else {
                $parameter = Array();
                $parameter["HOTEN"] = $params["HOTEN"];
                $parameter["CMND"] = $params["CMND"];
                $parameter["NGHE"] = $params["NGHE"];
                $parameter["SOCHUNGCHIHANHNGHE"] = $params["SOCHUNGCHIHANHNGHE"];
                $parameter["BAOHIEMXAHOI_SO"] = $params["BAOHIEMXAHOI_SO"];
                $parameter["BAOHIEMXAHOI_NGAY"] = TVTKCommon::VNDateToMySQLDate($params["BAOHIEMXAHOI_NGAY"]);
                $parameter["BAOHIEMXAHOI_NOICAP"] = $params["BAOHIEMXAHOI_NOICAP"];
                $parameter["ID_LV"] = $params["ID_LV"];
                $parameter["DIEM"] = $params["DIEM"];
                $parameter["GHICHU"] = $params["GHICHU"];
                $params["ID_NLNS"] = TVGS::InsertNangLucNhanSuByID_NLNS($parameter, $this->view->tvtk["ID_BK2"]);
                $is_new = 1;
            }
        }

        // Update DIEM_NANGLUCNHANSU
        TVGS::UpdateTVGSM2DIEM_NANGLUCNHANSU($this->view->tvtk["ID_BK2"]);

        if ($is_new != 2) {
            $nlns = TVGS::SelectNangLucNhanSuByID_NLNS($params["ID_NLNS"], $this->view->tochuc["ID_TC"]);
        } else {
            $nlns = Array();
            $nlns["ID_NLNS"] = $params["ID_NLNS"];
        }
        $nlns["IS_NEW"] = $is_new;
        echo json_encode($nlns);

        exit;
    }

    public function tvgsbuoc2loadnlnsAction() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        $params = $this->getRequest()->getParams();

        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);

        // Chi load nhan su neu dung la cua to chuc dang login
        $nlns = TVGS::SelectNangLucNhanSuByID_NLNS($params["id"], $this->view->tochuc["ID_TC"]);
        echo json_encode($nlns);
        exit;
    }

    public function tvgsbuoc3Action() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();

        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        $this->view->tvtk = TVGS::SelectLastTVGSByID_TC($this->view->tochuc["ID_TC"]);
        $this->view->level = TVGS::SelectNangLucKhacListByID_BM2($this->view->tvtk["ID_BK2"]);
        $this->view->doanhthu = TVGS::SelectDoanhThuListByID_BM2($this->view->tvtk["ID_BK2"], date("Y") - 4, date("Y"));
    }

    public function tvgsbuoc3savedraftAction() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        $params = $this->getRequest()->getParams();

        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        $this->view->tvtk = TVTK::SelectLastTVTKByID_TC($this->view->tochuc["ID_TC"]);
        $this->view->level = TVGS::SelectNangLucKhacListByID_BM2($this->view->tvtk["ID_BK1"]);
        $this->view->doanhthu = TVGS::SelectDoanhThuListByID_BM2($this->view->tvtk["ID_BK1"], date("Y") - 4, date("Y"));

        // get diem tvtk
        foreach ($this->view->level as $itemlevel) {
            TVGS::UpdateNangLucKhacByID_LV($itemlevel["ID_NLK"], $itemlevel["ID_LV"], $params["TVTKKHACDIEM" . $itemlevel["ID_LV"]], $params["TVTKKHACGHICHU" . $itemlevel["ID_LV"]], $this->view->tvtk["ID_BK2"]);
        }

        $trungbinh5nam = 0;
        for ($y = date('Y') - 4; $y <= date('Y'); $y++) {
            TVGS::UpdateDoanhThuByID_BM2($this->view->tvtk["ID_BK2"], $y, $params["ID_DT_YEAR_" . $y], $this->view->tochuc["ID_TC"]);
            $trungbinh5nam += $params["ID_DT_YEAR_" . $y];
        }

        $trungbinh5nam = ($trungbinh5nam / 5.0) * 2.0;

        TVGS::UpdateTVGSM2DIEM_DOANHTHU(Array("DIEM_DOANHTHU" => $trungbinh5nam), $this->view->tvtk["ID_BK2"]);

        echo 1;
        exit;
    }

    public function tvgsbuoc4Action() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();

        $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        $this->view->tvtk = TVGS::SelectLastTVGSByID_TC($this->view->tochuc["ID_TC"]);
        $this->view->duan = TVGS::SelectDuAnListByID_BM2($this->view->tvtk["ID_BK2"], date("Y") - 4, date("Y"));

        $this->view->countlevelnhomcapcongtrinh = Level::SelectLevelCountByTYPE('TVGS_NHOMCAPCONGTRINH');
        $this->view->levelnhomcapcongtrinh = Level::SelectLevelListByTYPE('TVGS_NHOMCAPCONGTRINH');

        $this->view->countlevelthongtindichvu = Level::SelectLevelCountByTYPE('TVGS_THONGTINDICHVU');
        $this->view->levelthongtindichvu = Level::SelectLevelListByTYPE('TVGS_THONGTINDICHVU');

        $this->view->countlevelthongtinbosung = Level::SelectLevelCountByTYPE('TVGS_THONGTINBOSUNG');
        $this->view->levelthongtinbosung = Level::SelectLevelListByTYPE('TVGS_THONGTINBOSUNG');
    }
    
    public function tvgsbuoc4savedraftAction() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        $params = $this->getRequest()->getParams();

        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
        $this->view->tvtk = TVGS::SelectLastTVGSByID_TC($this->view->tochuc["ID_TC"]);
        $is_new = 0;

        if ($params["delete"] > 0) {
            $duan = TVGS::SelectDuAnByID_DuAn($params["delete"], $this->view->tochuc["ID_TC"]);
            if ($duan["STATUS"] == 2) {
                TVGS::DeleteDuAnByID_DuAn($params["delete"], $this->view->tvtk["ID_BK2"], 0);
                $is_new = 2;
            } else {
                TVGS::DeleteDuAnByID_DuAn($params["delete"], $this->view->tvtk["ID_BK2"], 1);
            }

            $params["ID_DUAN"] = $params["delete"];
        } else {
            
            if ($params["DIEM_DUAN"] == 0) {
                $duan = Array();
                $duan["msg"] = "Chưa nhập Điểm";
                echo json_encode($duan);
                exit;
            }

            if ($params["TENDUAN"] == "") {
                $duan = Array();
                $duan["msg"] = "Chưa nhập tên dự án";
                echo json_encode($duan);
                exit;
            }
            // che do edit
            if ($params["ID_DUAN"] > 0) {
                $parameter = Array();
                $parameter["TENDUAN"] = $params["TENDUAN"];
                $parameter["NAMDUAN"] = $params["NAMDUAN"];
                $parameter["CAPDAUTU"] = $params["CAPDAUTU"];
                $parameter["CHUDAUTU"] = $params["CHUDAUTU"];
                $parameter["DAIDIENDAUTU"] = $params["DAIDIENDAUTU"];
                $parameter["LOAITYPE"] = $params["LOAITYPE"];
                $parameter["NHOMCAPCONGTRINH"] = $params["NHOMCAPCONGTRINH"];
                $parameter["GHICHU_NHOMCAPCONGTRINH"] = $params["GHICHU_NHOMCAPCONGTRINH"];
                $parameter["THONGTINDICHVU"] = $params["THONGTINDICHVU"];                
                $parameter["GHICHU_THONGTINDICHVU"] = $params["GHICHU_THONGTINDICHVU"];
                $parameter["THONGTINBOSUNG"] = $params["THONGTINBOSUNG"];
                $parameter["GHICHU_THONGTINBOSUNG"] = $params["GHICHU_THONGTINBOSUNG"];
                $parameter["DIEM_DUAN"] = $params["DIEM_DUAN"];
                TVGS::UpdateDuAnByID_DUAN($parameter, $params["ID_DUAN"], $this->view->tvtk["ID_BK2"]);
            } else {
                $parameter = Array();
                $parameter["TENDUAN"] = $params["TENDUAN"];
                $parameter["NAMDUAN"] = $params["NAMDUAN"];
                $parameter["CAPDAUTU"] = $params["CAPDAUTU"];
                $parameter["CHUDAUTU"] = $params["CHUDAUTU"];
                $parameter["DAIDIENDAUTU"] = $params["DAIDIENDAUTU"];
                $parameter["LOAITYPE"] = $params["LOAITYPE"];
                $parameter["NHOMCAPCONGTRINH"] = $params["NHOMCAPCONGTRINH"];
                $parameter["GHICHU_NHOMCAPCONGTRINH"] = $params["GHICHU_NHOMCAPCONGTRINH"];
                $parameter["THONGTINDICHVU"] = $params["THONGTINDICHVU"];                
                $parameter["GHICHU_THONGTINDICHVU"] = $params["GHICHU_THONGTINDICHVU"];
                $parameter["THONGTINBOSUNG"] = $params["THONGTINBOSUNG"];
                $parameter["GHICHU_THONGTINBOSUNG"] = $params["GHICHU_THONGTINBOSUNG"];
                $parameter["DIEM_DUAN"] = $params["DIEM_DUAN"];
                $params["ID_DUAN"] = TVGS::InsertDuAnByID_DUAN($parameter, $this->view->tvtk["ID_BK2"]);
                $is_new = 1;
            }
        }

        // Update DIEMduan
        TVGS::UpdateTVGSM2DIEM_DUAN($this->view->tvtk["ID_BK2"]);

        if ($is_new != 2) {
            $duan = TVGS::SelectDuAnByID_DuAn($params["ID_DUAN"], $this->view->tochuc["ID_TC"]);
        } else {
            $duan = Array();
            $duan["ID_DUAN"] = $params["ID_DUAN"];
        }
        $duan["IS_NEW"] = $is_new;
        echo json_encode($duan);

        exit;
    }


    public function tvgsbuoc4loadduanAction() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        $params = $this->getRequest()->getParams();

        $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);

        // Chi load duan neu dung la cua to chuc dang login
        $duan= TVGS::SelectDuAnByID_DuAn($params["id"], $this->view->tochuc["ID_TC"]);
        echo json_encode($duan);
        exit;
    }
    public function viewtvgstrang1Action() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVGS::SelectTVGSByID($id);
            $this->view->level = Level::SelectLevelListByTYPE("TVGS_CAPHANG");
            $this->view->levelnangluckhac = TVGS::SelectNangLucKhacListByID_BM2($id);
            $this->view->doanhthu = TVGS::SelectDoanhThuListByID_BM2($id, date("Y") - 4, date("Y"));
            $this->view->nlns = TVGS::SelectNangLucNhanSuListByID_BM2($id);
        }
        else
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
            $this->view->tvtk = TVGS::SelectLastTVGSByID_TC($this->view->tochuc["ID_TC"]);
            $this->view->level = Level::SelectLevelListByTYPE("TVGS_CAPHANG");
            $this->view->levelnangluckhac = TVGS::SelectNangLucKhacListByID_BM2($this->view->tvtk["ID_BK2"]);
            $this->view->doanhthu = TVGS::SelectDoanhThuListByID_BM2($this->view->tvtk["ID_BK2"], date("Y") - 4, date("Y"));
            $this->view->nlns = TVGS::SelectNangLucNhanSuListByID_BM2($this->view->tvtk["ID_BK2"]);
        }
        $this->view->id = $id;
        $this->view->readonly = (int)$this->getRequest()->getParam('readonly');
    }

    public function viewtvgstrang2Action() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVGS::SelectTVGSByID($id);
            $this->view->duan = TVGS::SelectDuAnListByID_BM2($id, date("Y") - 4, date("Y"));
              
        }
        else
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
            $this->view->tvtk = TVGS::SelectLastTVGSByID_TC($this->view->tochuc["ID_TC"]);
            $this->view->duan = TVGS::SelectDuAnListByID_BM2($this->view->tvtk["ID_BK2"], date("Y") - 4, date("Y"));
        }
        $this->view->countlevelnhomcapcongtrinh = Level::SelectLevelCountByTYPE('TVGS_NHOMCAPCONGTRINH');
        $this->view->levelnhomcapcongtrinh = Level::SelectLevelListByTYPE('TVGS_NHOMCAPCONGTRINH');
        $this->view->countlevelthongtindichvu = Level::SelectLevelCountByTYPE('TVGS_THONGTINDICHVU');
        $this->view->levelthongtindichvu = Level::SelectLevelListByTYPE('TVGS_THONGTINDICHVU');
        $this->view->countlevelthongtinbosung = Level::SelectLevelCountByTYPE('TVGS_THONGTINBOSUNG');
        $this->view->levelthongtinbosung = Level::SelectLevelListByTYPE('TVGS_THONGTINBOSUNG');
        $this->view->id = $id;
        $this->view->readonly = (int)$this->getRequest()->getParam('readonly');
    }
    public function viewexceltvgstrang1Action() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        header("Content-Type: application/vnd.ms-excel; name='excel'");
        header("Content-Disposition: attachment; filename=tvtktrang1.xls;");
        header("Pragma: no-cache");
        header("Expires: 0");
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVGS::SelectTVGSByID($id);
            $this->view->level = Level::SelectLevelListByTYPE("TVTK_CAPHANG");
            $this->view->levelnangluckhac = TVGS::SelectNangLucKhacListByID_BM2($id);
            $this->view->doanhthu = TVGS::SelectDoanhThuListByID_BM2($id, date("Y") - 4, date("Y"));
            $this->view->nlns = TVGS::SelectNangLucNhanSuListByID_BM2($id);
        }
        else
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
            $this->view->tvtk = TVGS::SelectLastTVGSByID_TC($this->view->tochuc["ID_TC"]);
            $this->view->level = Level::SelectLevelListByTYPE("TVTK_CAPHANG");
            $this->view->levelnangluckhac = TVGS::SelectNangLucKhacListByID_BM2($this->view->tvtk["ID_BK2"]);
            $this->view->doanhthu = TVGS::SelectDoanhThuListByID_BM2($this->view->tvtk["ID_BK2"], date("Y") - 4, date("Y"));
            $this->view->nlns = TVGS::SelectNangLucNhanSuListByID_BM2($this->view->tvtk["ID_BK2"]);
        }
        $this->view->id = $id;
        $this->view->readonly = (int)$this->getRequest()->getParam('readonly');
    }

    public function viewexceltvgstrang2Action() {
        $this->_helper->layout->disableLayout();
        $auth = Zend_Registry::get('auth');
        $user = $auth->getIdentity();
        header("Content-Type: application/vnd.ms-excel; name='excel'");
        header("Content-Disposition: attachment; filename=tvtktrang2.xls;");
        header("Pragma: no-cache");
        header("Expires: 0");
        $id=$this->getRequest()->getParam('id');
        if($id)
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID($id);
            $this->view->tvtk = TVGS::SelectTVGSByID($id);
            $this->view->duan = TVGS::SelectDuAnListByID_BM2($id, date("Y") - 4, date("Y"));
              
        }
        else
        {
            $this->view->linhvuc = LinhVuc::SelectLinhVucList("");
            $this->view->tochuc = ToChuc::SelectToChucByID_U($user->ID_U);
            $this->view->tvtk = TVGS::SelectLastTVGSByID_TC($this->view->tochuc["ID_TC"]);
            $this->view->duan = TVGS::SelectDuAnListByID_BM2($this->view->tvtk["ID_BK2"], date("Y") - 4, date("Y"));
        }
        $this->view->countlevelnhomcapcongtrinh = Level::SelectLevelCountByTYPE('TVGS_NHOMCAPCONGTRINH');
        $this->view->levelnhomcapcongtrinh = Level::SelectLevelListByTYPE('TVGS_NHOMCAPCONGTRINH');
        $this->view->countlevelthongtindichvu = Level::SelectLevelCountByTYPE('TVGS_THONGTINDICHVU');
        $this->view->levelthongtindichvu = Level::SelectLevelListByTYPE('TVGS_THONGTINDICHVU');
        $this->view->countlevelthongtinbosung = Level::SelectLevelCountByTYPE('TVGS_THONGTINBOSUNG');
        $this->view->levelthongtinbosung = Level::SelectLevelListByTYPE('TVGS_THONGTINBOSUNG');
        $this->view->id = $id;
        $this->view->readonly = (int)$this->getRequest()->getParam('readonly');
    }
}
